package com.yc.testupload;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import com.yc.testupload.util.DatabaseUtil;

/**
 * MySQL连接诊断工具类
 * 提供更详细的MySQL连接参数测试和诊断功能
 */
public class MySQLConnectionTester {
    
    public static void main(String[] args) {
        System.out.println("=== MySQL连接详细诊断工具 ===");
        
        // 获取当前数据库配置
        Properties props = DatabaseUtil.getProperties();
        String url = props.getProperty("jdbc.url", "");
        String username = props.getProperty("jdbc.username", "");
        String password = props.getProperty("jdbc.password", "");
        String driver = props.getProperty("jdbc.driver", "");
        
        System.out.println("\n当前配置信息：");
        System.out.println("驱动类: " + driver);
        System.out.println("连接URL: " + url);
        System.out.println("用户名: " + username);
        System.out.println("密码: 【已隐藏】");
        
        // 测试基本连接
        System.out.println("\n=== 测试基本连接 ===");
        testConnection(url, username, password, driver);
        
        // 测试连接超时参数
        System.out.println("\n=== 测试连接超时参数 ===");
        testConnectionTimeout(url, username, password, driver);
        
        // 测试连接属性参数
        System.out.println("\n=== 测试增强连接参数 ===");
        testEnhancedConnection(url, username, password, driver);
        
        // 测试TCP/IP连接参数
        System.out.println("\n=== 测试TCP连接参数 ===");
        testTcpConnection(url, username, password, driver);
        
        System.out.println("\n=== 诊断完成 ===");
    }
    
    /**
     * 测试基本数据库连接
     */
    private static void testConnection(String url, String username, String password, String driver) {
        try {
            // 注册驱动
            Class.forName(driver);
            System.out.println("驱动注册成功");
            
            // 尝试连接
            long startTime = System.currentTimeMillis();
            Connection conn = DriverManager.getConnection(url, username, password);
            long endTime = System.currentTimeMillis();
            
            if (conn != null && !conn.isClosed()) {
                System.out.println("连接成功！耗时: " + (endTime - startTime) + "ms");
                conn.close();
            }
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败: " + e.getMessage());
        } catch (SQLException e) {
            System.out.println("连接失败: " + e.getMessage());
            System.out.println("SQL状态: " + e.getSQLState());
            System.out.println("错误代码: " + e.getErrorCode());
        }
    }
    
    /**
     * 测试连接超时参数
     */
    private static void testConnectionTimeout(String baseUrl, String username, String password, String driver) {
        try {
            Class.forName(driver);
            
            // 修改URL，添加连接超时参数
            String timeoutUrl = baseUrl;
            if (!baseUrl.contains("?")) {
                timeoutUrl += "?";
            } else if (!baseUrl.endsWith("&")) {
                timeoutUrl += "&";
            }
            timeoutUrl += "connectTimeout=30000&socketTimeout=60000";
            
            System.out.println("使用超时参数: connectTimeout=30000, socketTimeout=60000");
            System.out.println("新URL: " + timeoutUrl);
            
            long startTime = System.currentTimeMillis();
            Connection conn = DriverManager.getConnection(timeoutUrl, username, password);
            long endTime = System.currentTimeMillis();
            
            if (conn != null && !conn.isClosed()) {
                System.out.println("带超时参数连接成功！耗时: " + (endTime - startTime) + "ms");
                conn.close();
            }
        } catch (Exception e) {
            System.out.println("带超时参数连接失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试增强连接参数
     */
    private static void testEnhancedConnection(String baseUrl, String username, String password, String driver) {
        try {
            Class.forName(driver);
            
            // 添加增强稳定性的参数
            String enhancedUrl = baseUrl;
            if (!baseUrl.contains("?")) {
                enhancedUrl += "?";
            } else if (!baseUrl.endsWith("&")) {
                enhancedUrl += "&";
            }
            enhancedUrl += "autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
            
            System.out.println("使用增强参数: autoReconnect=true, maxReconnects=10");
            
            long startTime = System.currentTimeMillis();
            Connection conn = DriverManager.getConnection(enhancedUrl, username, password);
            long endTime = System.currentTimeMillis();
            
            if (conn != null && !conn.isClosed()) {
                System.out.println("增强连接成功！耗时: " + (endTime - startTime) + "ms");
                conn.close();
            }
        } catch (Exception e) {
            System.out.println("增强连接失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试TCP连接参数
     */
    private static void testTcpConnection(String baseUrl, String username, String password, String driver) {
        try {
            Class.forName(driver);
            
            // 添加TCP优化参数
            String tcpUrl = baseUrl;
            if (!baseUrl.contains("?")) {
                tcpUrl += "?";
            } else if (!baseUrl.endsWith("&")) {
                tcpUrl += "&";
            }
            tcpUrl += "useSSL=false&cachePrepStmts=true&tcpKeepAlive=true";
            
            System.out.println("使用TCP优化参数: useSSL=false, tcpKeepAlive=true");
            
            long startTime = System.currentTimeMillis();
            Connection conn = DriverManager.getConnection(tcpUrl, username, password);
            long endTime = System.currentTimeMillis();
            
            if (conn != null && !conn.isClosed()) {
                System.out.println("TCP优化连接成功！耗时: " + (endTime - startTime) + "ms");
                conn.close();
            }
        } catch (Exception e) {
            System.out.println("TCP优化连接失败: " + e.getMessage());
        }
    }
}